package com.ruoyi.common.utils;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.security.SecureRandom;

public class CommonUtils {
    private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);
    /**二维数组遍历*/
    public static Integer getCzGoodsId(Integer[][] czGoodsFee, BigDecimal fee){
        Integer goodsId = null;
        try {
            for(int i=0;i<czGoodsFee.length;i++){
                Integer[] aa = czGoodsFee[i];
                if(aa[0].doubleValue() == fee.doubleValue()){
                    goodsId = aa[1];
                    break;
                }
            }
        }catch (Exception e){
            log.error("获取赠送金额异常:{}",e.getMessage());
        }
        return goodsId;
    }
    public static boolean isIDNumber(String IDNumber) {
        if (IDNumber == null || "".equals(IDNumber)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
                "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

        boolean matches = IDNumber.matches(regularExpression);
        //判断第18位校验值
        if (matches) {
            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    //前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    //这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        System.out.println("身份证最后一位:" + String.valueOf(idCardLast).toUpperCase() +
                                "错误,正确的应该是:" + idCardY[idCardMod].toUpperCase());
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("异常:" + IDNumber);
                    return false;
                }
            } else {
                return false;
            }
        }
        return matches;
    }

    public static Response error_code(String error_code){
        Response response = null;
        switch(error_code){
            case "210301" :
                response =  Response.fail(Constants.FAILCODE,"查询无此记录");
                break;
            case "210304" :
                response =  Response.fail(Constants.FAILCODE,"参数异常");
                break;
            case "210305" :
                response =  Response.fail(Constants.FAILCODE,"服务商网络异常，请重试");
                break;
            case "210306" :
                response =  Response.fail(Constants.FAILCODE,"数据源异常");
                break;
            case "210307" :
                response =  Response.fail(Constants.FAILCODE,"参数错误:sign检验失败");
                break;
            case "210308" :
                response =  Response.fail(Constants.FAILCODE,"订单号格式错误");
                break;
            case "10001" :
                response =  Response.fail(Constants.FAILCODE,"错误的请求KEY");
                break;
            case "10002" :
                response =  Response.fail(Constants.FAILCODE,"该KEY无请求权限");
                break;
            case "10003" :
                response =  Response.fail(Constants.FAILCODE,"KEY过期");
                break;
            case "10004" :
                response =  Response.fail(Constants.FAILCODE,"错误的OPENID");
                break;
            case "10005" :
                response =  Response.fail(Constants.FAILCODE,"应用未审核超时，请提交认证");
                break;
            case "10007" :
                response =  Response.fail(Constants.FAILCODE,"未知的请求源");
                break;
            case "10008" :
                response =  Response.fail(Constants.FAILCODE,"被禁止的IP");
                break;
            case "10009" :
                response =  Response.fail(Constants.FAILCODE,"被禁止的KEY");
                break;
            case "10011" :
                response =  Response.fail(Constants.FAILCODE,"当前IP请求超过限制 ");
                break;
            case "10012" :
                response =  Response.fail(Constants.FAILCODE,"请求超过次数限制");
                break;
            case "10013" :
                response =  Response.fail(Constants.FAILCODE,"测试KEY超过请求限制 ");
                break;
            case "10014" :
                response =  Response.fail(Constants.FAILCODE,"系统内部异常");
                break;
            case "10020" :
                response =  Response.fail(Constants.FAILCODE,"接口维护");
                break;
            case "10021" :
                response =  Response.fail(Constants.FAILCODE,"接口停用 ");
                break;
        }
        return response;
    }

    public static String generateRandomString(int length) {
        String ALLOWED_CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        SecureRandom random = new SecureRandom();
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(ALLOWED_CHARACTERS.length());
            char randomChar = ALLOWED_CHARACTERS.charAt(randomIndex);
            stringBuilder.append(randomChar);
        }
        return stringBuilder.toString();
    }
}
